package com.d3works.apps.businessproject.service;

import com.d3works.apps.account.db.AnalyticMoveLine;
import com.d3works.apps.base.service.CurrencyService;
import com.d3works.apps.base.service.ProductCompanyService;
import com.d3works.apps.base.service.app.AppBaseService;
import com.d3works.apps.base.service.tax.AccountManagementService;
import com.d3works.apps.contract.db.Contract;
import com.d3works.apps.contract.db.ContractLine;
import com.d3works.apps.contract.service.ContractLineServiceImpl;
import com.d3works.apps.project.db.Project;
import com.google.inject.Inject;
import java.util.List;

public class ContractLineServiceProjectImpl extends ContractLineServiceImpl {

  @Inject
  public ContractLineServiceProjectImpl(
      AppBaseService appBaseService,
      AccountManagementService accountManagementService,
      CurrencyService currencyService,
      ProductCompanyService productCompanyService) {
    super(appBaseService, accountManagementService, currencyService, productCompanyService);
  }

  @Override
  public ContractLine createAnalyticDistributionWithTemplate(
      ContractLine contractLine, Contract contract) {
    contractLine = super.createAnalyticDistributionWithTemplate(contractLine, contract);

    Project project = contract.getProject();

    if (project != null) {
      List<AnalyticMoveLine> analyticMoveLineList = contractLine.getAnalyticMoveLineList();

      analyticMoveLineList.forEach(analyticMoveLine -> analyticMoveLine.setProject(project));
      contractLine.setAnalyticMoveLineList(analyticMoveLineList);
    }
    return contractLine;
  }
}
